﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.成品出库单.service;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.Const;
using DXApplicationTangche.UC.门店出入库.UC;
using DXApplicationTangche.UC.成衣进销存.service;
using static DXApplicationTangche.UC.成衣进销存.service.成衣进销存Service;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.report;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.UC.钉钉;
using System.IO;
using DXApplicationTangche.UC.门店出入库.Service;

namespace DXApplicationTangche.UC.成品出库单
{
    public partial class Form抖音退货入库 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockInOutDetailDTO> StockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO stockInOutDetailDTO = new StockInOutDetailDTO();
        private StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO();
        private StockInOutDetailDTO otherStockInOutDetailDTO = new StockInOutDetailDTO();
        private List<StockInOutDetailDTO> otherStockInOutDetailDTOs = new List<StockInOutDetailDTO>();

        private String sessionId;
        public Form抖音退货入库()
        {
            InitializeComponent();
            this.sessionId = new FunctionHelper().Uuid;
        }

        private void textBox扫描_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox扫描.Text))
                {
                    try
                    {
                        //  其他入库
                        this.otherStockInOutDetailDTO = 出入库Service.otherStockIn(this.textBox扫描.Text.Trim(), "8");
                        try { this.otherStockInOutDetailDTO.styleSizeCd = this.otherStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        if (!String.IsNullOrEmpty(this.otherStockInOutDetailDTO.barcode))
                        {
                            if (this.otherStockInOutDetailDTOs.Count > 0)
                            {
                                if (!this.otherStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.otherStockInOutDetailDTO.barcode)))
                                {
                                    this.otherStockInOutDetailDTOs.Add(this.otherStockInOutDetailDTO);
                                }
                            }
                            else
                            {
                                this.otherStockInOutDetailDTOs.Add(this.otherStockInOutDetailDTO);
                            }
                        }
                        this.gridControl抖音退货入库.DataSource = this.otherStockInOutDetailDTOs;
                        this.gridView抖音退货入库.RefreshData();
                        this.gridView抖音退货入库.BestFitColumns();
                        this.textBox扫描.Clear();
                    }
                    catch (Exception ex)
                    {
                        this.otherStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (this.otherStockInOutDetailDTOs.Count > 0)
            {
                try
                {
                    this.出库单DTO = new StockInOutTitleDTO(
                                        this.sessionId,
                                        DateTime.Now,
                                        "1",
                                        StockInOutTypeConst.DouyinStockIn,
                                        "fengjinyue01",
                                        "冯金月",
                                        this.otherStockInOutDetailDTOs.Count,
                                        "8",
                                        "退回冻结存货",
                                        "抖音退货"
                                    );
                    出库单导出DTO 出库单导出DTO = new 出库单导出DTO(this.出库单DTO, this.otherStockInOutDetailDTOs);
                    String str = FunctionHelper.JsonSerialization(出库单导出DTO);
                    XtraReport抖音退货单 report = new XtraReport抖音退货单(出库单导出DTO);
                    report.抖音退货();
                    try
                    {
                        //  上传pdf:
                        string saveDirectoryPath = @"pdf\";
                        //  1.先本地生成文件
                        report.ExportToPdf(saveDirectoryPath + this.sessionId + @".pdf");
                        //  2.上传本地文件
                        String re = 成品出库单Service.uploadStockInOutFileByCode(this.sessionId, saveDirectoryPath + this.sessionId + @".pdf");
                        //  3.删除本地文件
                        File.Delete(saveDirectoryPath + re);
                        //  钉钉
                        钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.抖音退货入库, this.出库单DTO, re).build("17866554337",409162359L));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
                    }
                    //  显示
                    ReportPrintTool tool = new ReportPrintTool(report);
                    tool.ShowPreview();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //this.StockInOutDetailDTOs.Clear();
                //this.sessionId = new FunctionHelper().Uuid;
                this.gridControl抖音退货入库.DataSource = this.otherStockInOutDetailDTOs;
                this.gridView抖音退货入库.RefreshData();
                this.gridView抖音退货入库.BestFitColumns();
            }
            else
            {
                MessageBox.Show("无确认数据");
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void gridView成品出库_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
    }
}